package com.lab303.powerproject.service.impl;

import com.lab303.powerproject.entity.PredictData;
import com.lab303.powerproject.entity.PredictElectricity;
import com.lab303.powerproject.repository.PredictDataRepository;
import com.lab303.powerproject.repository.PredictElectricityRepository;
import com.lab303.powerproject.service.PredictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class PredictDataServiceImpl implements PredictDataService {

    @Autowired
    private PredictDataRepository pd;

    @Autowired
    private PredictElectricityRepository electricityRepository;

    @Override
    public Double[] findPredictDataByDate() {
        List<PredictData> predictDataByDeviceId = pd.findPredictDataByDeviceId();
        if (predictDataByDeviceId!=null){
            PredictData predictData = predictDataByDeviceId.get(0);
            String[] split = predictData.getData().split(",");
            Double[] data = new Double[split.length];
            for (int i = 0;i<split.length;i++){
                data[i] = Double.parseDouble(split[i]);
            }
            return data;
        }else
            return null;
    }

    @Override
    public HashMap<Integer, double[]> getAllPredictData() {
        HashMap<Integer, double[]> rets = new HashMap<Integer, double[]>();
        List<PredictData> pds =  pd.findAll();
        for(int i =0;i<pds.size();++i){
            int id = pds.get(i).getPredictDeviceId();
            String[] split = pds.get(i).getData().split(",");
            double[] data = new double[split.length];
            for (int j = 0;j<split.length;j++){
                data[j] = Double.parseDouble(split[j]);
            }
            rets.put(id,data);
        }
        return rets;
    }

    @Override
    public float[] getPredictDataByDateAndDeviceId(Integer deviceId) {
        PredictData predictData = pd.getPredictDataByDateAndDEiveId(deviceId);
        String[] split = predictData.getData().trim().split(",");
        float[] res = new float[split.length];
        for (int i = 0;i<res.length;i++){
            res[i] = Float.parseFloat(split[i]);
        }
        return res;
    }

    @Override
    public float[] getPredictElectricityDataByDateAndId(Integer id) {
        PredictElectricity electricity = electricityRepository.getPredictElectricityByDateAnId(id);
        String[] split = electricity.getData().trim().split(",");
        float[] res = new float[split.length];
        for (int i = 0;i<res.length;i++){
            res[i] = Float.parseFloat(split[i]);
        }
        return res;
    }

    @Override
    public PredictData getPredictPower(){
        PredictData predictDataByDate = pd.getPredictDataByDate();
        return predictDataByDate;
    }

    @Override
    public PredictElectricity getPredictElectricity() {
        PredictElectricity predictElectricityByDate = electricityRepository.getPredictElectricityByDate();
        return predictElectricityByDate;
    }

    @Override
    public float[] getPredictElectricityDataByDayAndId(String day, Integer id) {
        PredictData andDayAnId = pd.findPredictDataByDateAndDayAnId(day, id);
        float[] res = new float[24];
        if (andDayAnId!=null){
            String[] split = andDayAnId.getData().split(",");
            for (int i =0;i<split.length;i++){
                res[i] = Float.parseFloat(split[i]);
            }
        }
        return res;
    }

    @Override
    public float[] getPredictDlByDayAndId(String day, Integer id) {
        PredictElectricity andDayAnId = electricityRepository.findPredictDlByDateAndId(day, id);
        float[] res = new float[24];
        if (andDayAnId!=null){
            String[] split = andDayAnId.getData().split(",");
            for (int i =0;i<split.length;i++){
                res[i] = Float.parseFloat(split[i]);
            }
        }
        return res;
    }


}
